class Solution {
    /**
     *  枚举+Hash
     * 该算法保证了每个元素最多遍历2次
     */
    public int longestConsecutive(int[] nums) {
        HashSet<Integer> st = new HashSet<>();
        int ans = 0;
        for (int i : nums) {
            st.add(i);
        }
        for (int i : nums) {
            int l = i - 1, r = i + 1;
            while (st.remove(r)) {
                r++;
            }
            while (st.remove(l)) {
                l--;
            }
            ans = Math.max(ans, r - l - 1);
        }
        return ans;
    }
}